package servletClasses;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.ws.Response;

import connection.DBConnection;

import daoClasses.InsertIUDao;
import daoClasses.LoginDAO;
import daoClasses.Query;

import beanClasses.IndividualUser;
import beanClasses.loginBean;
import beanUtils.BeanUtilClass;

/**
 * Servlet implementation class loginServlet
 */
@WebServlet("/loginServlet")
public class loginServlet extends HttpServlet 
{
	private static final long serialVersionUID = 1L;
    
	Connection con=null;
	Query q=new Query();
	String uname1;
	String password1;
	String uname;
	String password;
	String fname;
	ResultSet rs = null;
	ResultSet rs1 = null;
	Statement stat=null;
	String status=null;
	DBConnection db;
	public loginServlet() 
	{
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
			doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		loginBean log=new loginBean();
		BeanUtilClass.populateBean(log, request);
		HttpSession session;	
		boolean res;
		uname = log.getAcc_holder_id();
    	password = log.getPwd();
    	
    	System.out.println("name from login  "+log.getAcc_holder_id());
		System.out.println("pass from login  "+log.getPwd());
		 	
	    	try {
				con = DBConnection.createConnection();
				stat=con.createStatement();
			} catch (SQLException e) {
					e.printStackTrace();
			}	   	
		   	if(uname.contains("IU"))
		   	{
		   		res=IU_Search();
		   		//if(status.equals("success Status"))
		   		if(res)
				{ 			
		   			System.out.println("Welcome 100rabh as a IU");
		   			session=request.getSession(true);
		   			session.setAttribute("uname1", uname1);
		   			session.setAttribute("fname", fname);
		   	  		response.sendRedirect("indivisual_client.jsp");
				}
		   		else
		   			response.sendRedirect("Login.jsp?msg=invalid");
		    }
		   	else if(uname.contains("BRC"))
		   	{ 
		   		res=BRC_Search();
		   		if(res)
				{
		   			System.out.println("Welcome 100rabh as a Broker's Client");
		   			session=request.getSession(true);
		   			session.setAttribute("uname1", uname1);
		   			session.setAttribute("fname", fname);
		   	  		response.sendRedirect("borker_client.jsp");
				}
		   		else
		   			response.sendRedirect("Login.jsp?msg=invalid");
		    }
		   	else if(uname.contains("BR"))
		   	{ 
		   		res=BR_Search();		   		
		   		if(res)
				{
		   			System.out.println("Welcome 100rabh as a broker");
		   			session=request.getSession(true);
		   			session.setAttribute("uname1", uname1);
		   			session.setAttribute("fname", fname);
		   	  		response.sendRedirect("broker.jsp");
				}
		   		else
		   			response.sendRedirect("Login.jsp?msg=invalid");
		    }
		   	else if(uname.contains("dm"))
		   	{ 
		   		res=admin_Search();
		   		
		   		if(res)
				{
		   			System.out.println("Welcome 100rabh as a admin");
		   			session=request.getSession(true);
		   			session.setAttribute("uname1", uname1);
		   			session.setAttribute("fname", fname);
		   	  		response.sendRedirect("admin.jsp");
				}
		   		else
		   			response.sendRedirect("Login.jsp?msg=invalid");
		    }
		    else
		    {
		    	System.out.println("Sorry please check user ID");
		    	response.sendRedirect("Login.jsp?msg=invalid");
		    }
}
	public boolean IU_Search()
	{
		System.out.println("In IU_search Fun");
		//System.out.println("Uname = "+uname);
		try {
			//pstmt = con.prepareStatement(q.loginUser(uname));
			//rs=stat.executeQuery("select acc_holder_id,pwd from acc_holder");
			rs=stat.executeQuery(q.loginUser(uname));
			
			//rs=pstmt.executeQuery();
			while(rs.next())
	    	{
	    		uname1=rs.getString("Acc_holder_id");
	    		password1 =rs.getString("pwd");
	    		
	    		if((uname.equals(uname1)) && (password.equals(password1) ))
	    		{
	    			fname=rs.getString("f_name");
	    			System.out.println("Your user name is " + uname1);
	    			//status="success Status";
	    			return true;
	    		}
	    		else{
	    			//status="Invalid Status";
	    			return false;
	    		}
	    		/*if(status.equals("success Status"))
	    		{	    		
	    			break;
	    		}*/
	    	}
		} 
		catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean BR_Search()
	{
		System.out.println("In BR_search Fun");		
		try {
			rs=stat.executeQuery("select broker_id,pwd from broker");
			while(rs.next())
			{
				uname1=rs.getString("broker_id");
				password1=rs.getString("pwd");
				if(uname1.equals(uname)&&password1.equals(password))
				{
					System.out.println("u r Success");
					return true;
				}
				else{
	    			System.out.println("u r UNsuccess");
	    			return false;
	    		}
			}
		} 
		catch (SQLException e) {	
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean BRC_Search()
	{
		System.out.println("In BRC_search Fun");		
		try {
			rs=stat.executeQuery("select broker_client_id,pwd from broker_client");
			while(rs.next())
			{
				uname1=rs.getString("broker_client_id");
				password1=rs.getString("pwd");
				System.out.println("IN While uname = "+uname1);	
				System.out.println("IN While pwd = "+password1);	
				if(uname1.equals(uname)&&password1.equals(password))
				{
					//status="success Status";
					return true;
				}
				else{
	    			//status="Invalid Status";
					return false; 
	    		}
			}
		} 
		catch (SQLException e) {	
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean admin_Search()
	{
		System.out.println("In admin_search Fun");		
		try {
			rs=stat.executeQuery("select adminuname,pwd from cdxadmin");
			while(rs.next())
			{
				uname1=rs.getString("adminuname");
				password1=rs.getString("pwd");
				if(uname1.equals(uname)&&password1.equals(password))
				{
					//status="success Status";
					return true;
				}
				else{
	    			//status="Invalid Status";
					return false;
	    		}
			}
		} 
		catch (SQLException e) {	
			e.printStackTrace();
		}
		return false;
	}
}